package samsung.sds.userManagement.service.impl;

import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import samsung.sds.departmentManagement.model.DepartmentEntity;
import samsung.sds.roleManagement.model.RoleManagementEntity;
import samsung.sds.roleManagement.service.RoleManagementService;
import samsung.sds.system.common.baseQuery.BaseMapper;
import samsung.sds.system.common.baseQuery.AbstractBaseService;
import samsung.sds.system.common.model.QueryParam;
import samsung.sds.system.utils.CommonTools;
import samsung.sds.userManagement.dao.UserMapper;
import samsung.sds.userManagement.model.UserEntity;
import samsung.sds.userManagement.service.UserManagementService;
/**
 * @author hao.wen
 * @craeteDate 2016-01-08
 * 
 * userManagement的service组件
 */
@Service
public class UserManagementServiceImpl  extends AbstractBaseService<UserEntity> implements UserManagementService {


	@Autowired
	private UserMapper userMapper;

	@Autowired
	private RoleManagementService roleManagementServiceImpl;
	
	/**
	 * 获取userMapper
	 */
	@Override
	protected BaseMapper<UserEntity> getMapper() {
		
		return userMapper;
	}

	/**
	 * 插入用户实体
	 * @param user 用户实体
	 * @return 插入的记录数
	 */
	public int insertEntity(UserEntity user){
		
		//设置实体的创建人、创建时间
		user.setCreate_user(CommonTools.getSessionUserName());		
		user.setCreate_time(CommonTools.getCurrentDateTime());	
		int insertNum = this.getMapper().insertEntity(user);
		//插入用户角色
		if(user.getRole_name()!=null && user.getRole_name()!=""){
			String[] roleArray = user.getRole_name().split(",");
			for(String i:roleArray){
				user.setRole_name(i);
				userMapper.insertSYS_USER_TO_ROLE(user);
			}
		}
		return insertNum;
	}

	/**
	 * 根据用户名获取该用户的角色
	 * @param username 用户名
	 * @return 用户角色集合
	 */
	public List<String> queryUserRole(String username){
		return userMapper.selectSYS_USER_TO_ROLE01(username);
	}
	
	/**
	 * 根据用户名获取该用户的所有角色对象
	 * @param username 用户名
	 * @return 角色对象的集合
	 */
	public List<RoleManagementEntity> getUserRoles(String username){
		QueryParam param = new QueryParam();
		
		//获取所有角色名称，准备发往前端
		List<RoleManagementEntity> roleList = roleManagementServiceImpl.queryList(param);
		
		//获取当前用户的角色
		List<String> roleArray = userMapper.selectSYS_USER_TO_ROLE01(username);

		Iterator<RoleManagementEntity> listi = roleList.iterator();
		
		//删除角色列表中所选对象已经具备的角色
		while(listi.hasNext()){
			RoleManagementEntity roleEntity = listi.next();
			if( roleArray.contains(roleEntity.getRole_name() )){
				listi.remove();
			}
		}
		
		return roleList;
		
	}
	
	/**
	 * 检查用户名是否重复
	 * @param username 用户名
	 * @return 是否重复
	 */
	public boolean checkUserNameRepeat(String username){
		String[] userStution =  username.split(",");
		String usernameNow = userStution[0];
		String userDotype = userStution[1];
		
		UserEntity user = new UserEntity();
		user.setUsername(usernameNow);
		user = findUniqueByProperty(user);
		if(user != null && "create".equals(userDotype)){
			return 	false;
		}
		return 	true;
	}
	
	/**
	 * 修改用户对象
	 * @param user 用户对象
	 * @return 修改影响的记录数
	 */
	public int updateEntity(UserEntity user){
		user.setLast_modify_user(CommonTools.getSessionUserName());	
		user.setLast_modify_time(CommonTools.getCurrentDateTime());
		
		//获取原先的创建人与创建时间
		UserEntity oldUser =  this.getMapper().findUniqueByProperty(user);
		user.setCreate_time(oldUser.getCreate_time());
		user.setCreate_user(oldUser.getCreate_user());
		
		if(oldUser.getPassword() .equals(user.getPassword())){
			user.setPassword("");
		}
		
		roleManagementServiceImpl.deleteRoleByUserName(user.getUsername());
		//判断是否有角色
		if(user.getRole_name()!=null && user.getRole_name()!=""){
			
			//判断
			String[] roleArray = user.getRole_name().split(",");
				for(String i:roleArray){
					user.setRole_name(i);
					userMapper.insertSYS_USER_TO_ROLE(user);
				}
		}
		
		return this.getMapper().updateEntity(user);
	}
	
	/**
	 * 根据username删除user对象
	 * @param user
	 * @return
	 */
	public int deleteEntityById(String username){
		
		roleManagementServiceImpl.deleteRoleByUserName(username);
		UserEntity user = new UserEntity();
		user.setUsername(username);
		user = this.getMapper().findUniqueByProperty(user);
		if(user==null)
			return -1;		
		return this.getMapper().deleteEntity(user);
	}
	
	/**
	 * 检查密码是否改变
	 * @param username 用户名
	 * @param password 加密后密码
	 * @return 是否改变
	 */
	public boolean checkPassword(String username,String password){
		
		UserEntity user = new UserEntity();
		user.setUsername(username);
		user = this.getMapper().findUniqueByProperty(user);
		//判断密码是否与原密码相同
		if(user != null && password .equals(user.getPassword())){
			
			return true;
		}
			return false;
	}

	/**
	 * 更新组织机构下的用户 
	 * @param user 用户对象
	 * @return 改变的记录数
	 */
	public int updateUser(String deptId, String[] userNames) {
		
		return userMapper.updateSYS_USER(deptId, userNames);
	}

	/**
	 * 查询组织机构下是否存在用户
	 * @param departmentEntities
	 * @return
	 */
	public int queryDepartmentUserCount( List<DepartmentEntity> departmentEntities ){
		
		return userMapper.selectSYS_USER(departmentEntities);
	}
	
	/**
	 * 更新用户组织机构
	 * @param newDeptId	用户将要归属为哪个组织机构的ID
	 * @param oldDeptId	用户以前归属的组织机构的ID
	 * @return
	 */
	public int updateUserDepartment(String newDeptId, String oldDeptId) {
		
		return userMapper.updateSYS_USER01(newDeptId, oldDeptId);
	}
	
	/**
	 * 修改用户对象
	 * @param user 用户对象
	 * @return 修改影响的记录数
	 */
	public int updatePersonalInfo(UserEntity user){	
		return super.updateEntity(user);
	}

}
